/**
 * 数据源相关的API服务
 */
import { request } from '@umijs/max';

// 类型定义
export interface DataSourceConfig {
  [key: string]: any;
}

export interface MySQLConfig {
  host: string;
  port: number;
  database: string;
  username: string;
  password: string;
  charset?: string;
}

export interface CSVConfig {
  file_path: string;
  encoding?: string;
  delimiter?: string;
  has_header?: boolean;
}

export interface ExcelConfig {
  file_path: string;
  sheet_name?: string;
  has_header?: boolean;
  start_row?: number;
}

export interface DataSourceCreate {
  name: string;
  type: 'mysql' | 'csv' | 'excel';
  config: DataSourceConfig;
}

export interface DataSourceUpdate {
  name?: string;
  config?: DataSourceConfig;
}

export interface DataSourceItem {
  id: number;
  name: string;
  type: 'mysql' | 'csv' | 'excel';
  config: DataSourceConfig;
  description?: string;
  user_id: number;
  created_at: string;
  updated_at: string;
}

export interface DataSourceList {
  items: DataSourceItem[];
  total: number;
  page: number;
  size: number;
  pages: number;
}

export interface ConnectionTestRequest {
  type: 'mysql' | 'csv' | 'excel';
  config: DataSourceConfig;
}

export interface ConnectionTestResponse {
  success: boolean;
  message: string;
  details?: any;
}

export interface DataPreviewRequest {
  data_source_id: number;
  limit?: number;
}

export interface DataPreviewResponse {
  columns: string[];
  data: any[][];
  total_rows?: number;
  sample_rows: number;
}

export interface DataSourceStatistics {
  total: number;
  mysql: number;
  csv: number;
  excel: number;
}

// API接口
export async function getDataSources(params: {
  page?: number;
  size?: number;
  search?: string;
  type?: string;
}): Promise<DataSourceList> {
  return request('/api/data-sources/', {
    method: 'GET',
    params,
  });
}

export async function getDataSource(id: number): Promise<DataSourceItem> {
  return request(`/api/data-sources/${id}`, {
    method: 'GET',
  });
}

export async function createDataSource(data: DataSourceCreate): Promise<DataSourceItem> {
  return request('/api/data-sources/', {
    method: 'POST',
    data,
  });
}

export async function updateDataSource(id: number, data: DataSourceUpdate): Promise<DataSourceItem> {
  return request(`/api/data-sources/${id}`, {
    method: 'PUT',
    data,
  });
}

export async function deleteDataSource(id: number): Promise<{ message: string }> {
  return request(`/api/data-sources/${id}`, {
    method: 'DELETE',
  });
}

export async function testConnection(data: ConnectionTestRequest): Promise<ConnectionTestResponse> {
  return request('/api/data-sources/test-connection', {
    method: 'POST',
    data,
  });
}

export async function previewData(data: DataPreviewRequest): Promise<DataPreviewResponse> {
  return request('/api/data-sources/preview', {
    method: 'POST',
    data,
  });
}

export async function uploadFile(file: File): Promise<{
  message: string;
  file_path: string;
  filename: string;
}> {
  const formData = new FormData();
  formData.append('file', file);
  
  return request('/api/data-sources/upload', {
    method: 'POST',
    data: formData,
  });
}

export async function getExcelSheets(filePath: string): Promise<{ sheets: string[] }> {
  return request(`/api/data-sources/excel/${encodeURIComponent(filePath)}/sheets`, {
    method: 'GET',
  });
}

export async function getDataSourceStatistics(): Promise<DataSourceStatistics> {
  return request('/api/data-sources/statistics', {
    method: 'GET',
  });
}
